package org.maven.dependencybrowser.model;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.apache.maven.model.Dependency;
import org.maven.dependencybrowser.assembler.AssembledModel;
import org.maven.dependencybrowser.controller.ArtifactResolver;
import org.maven.dependencybrowser.implementations.IProgressMeter;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PomFile extends Artifact
{
    protected List<Artifact> dependencies;
    protected AssembledModel model;

    public PomFile(AssembledModel model, Artifact parent, String scope) throws ArtifactInitialisationException
    {
        super(parent, model.getAssembledModel().getGroupId(), model.getAssembledModel().getArtifactId(), model.getAssembledModel().getVersion(), scope);
        this.model = model;
    }

    public void resolveDependencies(ArtifactResolver resolver, IProgressMeter progress, Stack<LeafDependency> path)
    {
        progress.resolving(this);
        dependencies = new ArrayList<Artifact>();
        // add the dependencies
        for (Object o : model.getAssembledModel().getDependencies())
        {
            Dependency dependency = (Dependency) o;
            /*
              Check to see if dependency contains in path
             */
            try
            {
                LeafDependency leafDependency = new LeafDependency(this, dependency, dependency.getScope());
                /**
                 * Prevent circular dependencies
                 */
                if (!path.contains(leafDependency))
                {
                    path.push(leafDependency);
                    Artifact artifact = resolver.resolveArtifact(this, dependency, progress, path);
                    if (artifact != null)
                    {
                        dependencies.add(artifact);
                    }
                }
            }
            catch (ArtifactInitialisationException e)
            {
                e.printStackTrace();
            }
        }
    }

    public AssembledModel getModel()
    {
        return model;
    }

    public boolean isLeaf()
    {
        return dependencies.size() == 0;
    }

    public List getDependencies()
    {
        return dependencies;
    }

    public int dependencyCount()
    {
        if (isLeaf())
        {
            return 1;
        }
        int dependencyCount = 0;
        for (Artifact artifact : dependencies)
        {
            dependencyCount += artifact.dependencyCount();
        }
        return dependencyCount;
    }

}
